<{include file="element/inc_header.html"}>
 
<div class="main-container" id="main-container">
	<script type="text/javascript">
		try{ace.settings.check('main-container' , 'fixed')}catch(e){}
	</script>

	<div class="main-container-inner">
		<div class="page-content">
			<div class="page-header">
				<h1>
					高级查询
					<small>
						<i class="icon-double-angle-right"></i>
						数据表格 &amp; 列表
					</small>
				</h1>
			</div><!-- /.page-header -->
			<div class="widget-box">
				<div class="widget-header">
					<h4>条件查询</h4>
					<div class="widget-toolbar no-border"> 
						<a href="/admin.php?c=order&a=index" class="btn btn-xs bigger btn-yellow">
							<i class="icon-double-angle-left"></i>
							返回列表
						</a> 
					</div>
				</div>
				<div class="widget-body">
					<div class="widget-main">
						<table>
							<tr>
								<td>字段名</td>
								<td>
									<select class="form-control" id="filed" name="filed">  
<{foreach from=$OrderFiled.field item=of key=key }>
										<option value="<{$key}>"><{$of.text}></option>
<{/foreach}>
									</select>
								</td>
								<td>操作符</td>
								<td>
									<select class="form-control" id="oper" name="oper">
										
<{foreach from=$OperList item=it key=key }>
										<option value="<{$key}>"><{$it}></option>
<{/foreach}>
									</select>
								</td>
								<td>条件值</td>
								<td>
									<input type="text" id="val" name="val" class="input-small" placeholder="条件值" />
								</td>
								<td>
									<button id="addbtn" type="button" class="btn btn-info btn-sm">
										<i class="icon-key bigger-110"></i>添加
									</button>
								</td>
							</tr>
						</table>	 
					</div>
				</div>
			</div> 	
			<div class="row">
				<div class="col-xs-12">
					<!-- PAGE CONTENT BEGINS -->
					<div class="table-responsive">
						<table id="sample-table-1" class="table table-striped table-bordered table-hover">
							<thead>
								<tr> 
									<th>描述</th>
									<th>操作符</th>
									<th>值</th> 
									<th>SQL</th>
									<th></th>
								</tr>
							</thead> 
							<tbody id="operbody"></tbody>
						</table> 
					</div>
					
					<div class="row"  style="margin-top: 0px;">
					<form class="form-inline">
						<input type="hidden" id="tablename" value="<{$TableName}>" />
						<input type="hidden" id="model" value="<{$ModelName}>" />
						<table>
							<tr>
								<td>&nbsp;</td>
								<td>导出功能描述信息</td> 
								<td>
									<input type="text" id="etitle" name="etitle" class="input-small" placeholder="导出功能描述信息" style="width: 250px;" />
								</td>
								<td>
									<button id="exoprtbtn" type="button" class="btn btn-primary btn-sm">
							<i class="icon-share-alt bigger-110"></i>好的,添加完成现在就去导出数据
						</button>
								</td>
							</tr>
						</table>							
					</form>  
					</div>
				</div> 
			</div><!-- /.row --> 
		</div><!-- /.page-content --> 
  
	</div><!-- /.main-container -->
</div>
<{include file="element/js_lib.html"}>
<script language="javascript">
var operHTML = '<div class="visible-md visible-lg hidden-sm hidden-xs btn-group">'
				//+'<button class="btn btn-xs btn-success"><i class="icon-ok bigger-120"></i></button>'
				//+'<button class="btn btn-xs btn-info"><i class="icon-edit bigger-120"></i></button>'
				+'<button class="btn btn-xs btn-danger dropline"><i class="icon-trash bigger-120"></i></button>'
				//+'<button class="btn btn-xs btn-warning"><i class="icon-flag bigger-120"></i></button>'
				+'</div>';
				
	jQuery(function(){
		$('#addbtn').click(function(){ 
			 var html = '<tr><td>'+ $("#filed").find("option:selected").text() +'</td><td>'+$("#oper").find("option:selected").text()+'</td><td>'+$('#val').val()+'</td><td class="opersqlline">';
			 var str = $("#filed").val()+' '+$("#oper").val();
			 if('like'==$("#oper").val() || 'not like'==$("#oper").val()){
			 	str += ' \'%'+ $("#val").val() +'%\' ';
			 }else if('in'==$("#oper").val() || 'not in'==$("#oper").val()){
			 	var tstr =  $("#val").val();
			 	tstr = tstr.split('|');
			 	if(tstr.length<2){
			 		alert('输入值有误！');
			 	}
			 	var tlist = '';
			 	$.each(tstr,function(i,d){
			 		tlist += ',\''+d+'\'';
			 	});
			 	str += ' ('+ tlist.substr(1) +') ';
			 }else if('between'==$("#oper").val() || 'not between'==$("#oper").val()){
			 	var tstr =  $("#val").val();
			 	tstr = tstr.split('|');
			 	if(tstr.length<2){
			 		alert('输入值有误！');
			 	}
			 	str += ' \''+tstr[0] +'\' and \''+ tstr[1]+'\' ' ;
			 }else{
			 	str += ' \''+ $("#val").val() +'\' ';
			 }
			 html += str + '</td><td>'+operHTML+'</td></tr>';
			 $('#operbody').append(html);
			 $('.dropline').unbind('click').bind('click',function(){
			 	$(this).parents('tr').remove();
			 });
		});
		
		$('#exoprtbtn').click(function(){
			var sql = 'select * from '+$('#tablename').val()+' where ' ;
			var wherestr = ' 1=1 '; 
			$.each($('.opersqlline'),function(i,d){
				wherestr += ' and '+$(d).text();
			});
			var data = {};
			data.sql = sql+wherestr;
			data.where = wherestr;
			data.tablename = $('#tablename').val();
			data.title = $('#etitle').val();
			data.model = $('#model').val();
			if(!data.title){
				alert('请输入文案信息!');
			}
			var url = '/admin.php?c=order&a=saveexport';
			$.post(
				url,data,function(d){
					alert(d.m);
					if(d.s) location.href = '/admin.php?c=order&a=searchlist'
				},'json'
			);
		});
		 
	})
</script> 